package com.zjq.springsocial.renren.android.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.zjq.springsocial.renren.android.R;
import com.zjq.springsocial.renren.android.RenrenAbstractAsyncActivity;
import com.zjq.springsocial.renren.api.User;

public class RenrenMainActivity extends RenrenAbstractAsyncActivity {

	protected static final String TAG = RenrenMainActivity.class.getSimpleName();
	
	private TextView showTest;
	
	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it
	 *            is null.</b>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		createMenuDrawer(R.layout.activity_renren_main);
		
		showTest = (TextView)findViewById(R.id.showTest);
	}
	
	private void showResult(User profile) {
		if (profile != null) {
			CharSequence text = getString(R.string.welcome_login_string) + profile.getName();
			showTest.setText(text);
		}
	}
	
	public void onStart(){
		super.onStart();
		new RenrenProfileTask().execute();
	}

	private class RenrenProfileTask extends AsyncTask<Void, Void, User>{
		
		@Override
		protected void onPreExecute() {
			showLoadingProgressDialog();
		}
		
		@Override
		protected User doInBackground(Void... params) {
			try {
				return renrenApi.getUserOperations().getUser();
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage(), e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(User profile) {
			dismissProgressDialog();
			showResult(profile);
		}
	}
}
